#include <stdlib.h>
#include <time.h>
#include "Reglesbase.h"
#include "Camp.hpp"

using namespace std;

Reglesbase::Reglesbase():Regles()
{

}

Reglesbase::~Reglesbase()
{
    //dtor
}


bool Reglesbase::attaquer(Territoire* T1,Territoire* T2, Camp* c, std::map<string, Client*> clients)
{
    vector<int> DesTerritoire1;
    DesTerritoire1=lancerDes(T1->getNbDesCourant());
    vector<int> DesTerritoire2;
    DesTerritoire2=lancerDes(T2->getNbDesCourant());

    for( std::map<string, Client*>::iterator tr = clients.begin(); tr != clients.end(); tr++)
    {
        if((*tr).second != NULL)
        {
            sf::Packet pp;
            pp << eGAME_LIST;
            (*tr).second->BR_partie->send( pp );

            pp.clear();
            pp << (sf::Uint8)DesTerritoire1.size() << (sf::Uint8)T1->getIndiceCamp() << (sf::Uint8)DesTerritoire2.size() << (sf::Uint8)T2->getIndiceCamp();
            (*tr).second->BR_partie->send( pp );
        }
    }



    int totalT1=0;
    int totalT2=0;
    for (vector<int>::iterator it=DesTerritoire1.begin(); it != DesTerritoire1.end(); ++it)
    {
        totalT1+=*it;

        for( std::map<string, Client*>::iterator tr = clients.begin(); tr != clients.end(); tr++)
        {
            if((*tr).second != NULL)
            {
                sf::Packet pp;
                pp << (sf::Uint8)(*it);
                (*tr).second->BR_partie->send( pp );
            }
        }
        //cout << (*it) << " ";
    }
	cout << endl;
    for (vector<int>::iterator it2=DesTerritoire2.begin(); it2 != DesTerritoire2.end(); ++it2)
    {
        totalT2+=*it2;

        for( std::map<string, Client*>::iterator tr = clients.begin(); tr != clients.end(); tr++)
        {
            if((*tr).second != NULL)
            {
                sf::Packet pp;
                pp << (sf::Uint8)(*it2);
                (*tr).second->BR_partie->send( pp );
            }
        }
        //cout << (*it2) << " ";
    }
    //cout << endl;

    cout << "T_atk_scr:" << totalT1 << " - T_def_scr:" << totalT2 << endl;
    bool res = comparer(totalT1,totalT2);
    if(res)
    {
	/*cout << "avant : ";

	for(int v=0;v<c[T1->getIndiceCamp()].nbTerritoire();v++)
	{
		cout << c[T1->getIndiceCamp()].territoires[v]->getIndice() << " : ";
	}
	cout << endl;

	for(int v=0;v<c[T2->getIndiceCamp()].nbTerritoire();v++)
	{
		cout << c[T2->getIndiceCamp()].territoires[v]->getIndice() << " : ";
	}

	cout << endl;
*/
        T2->setNbDesCourant(T1->getNbDesCourant()-1);
        T1->setNbDesCourant(1);
        //int w = T2->getIndiceCamp();
        T2->setIndiceCamp(T1->getIndiceCamp(), c);
/*
	cout << "apres : ";
	for(int v=0;v<c[T1->getIndiceCamp()].nbTerritoire();v++)
	{
		cout << c[T1->getIndiceCamp()].territoires[v]->getIndice() << " : ";
	}
	cout << endl;

	for(int v=0;v<c[w].nbTerritoire();v++)
	{
		cout << c[w].territoires[v]->getIndice() << " : ";
	}
	cout << endl;
*/
    }
    else
    {
        T1->setNbDesCourant(1);
    }

    return res;

}

vector<int> Reglesbase::lancerDes(int nbDes)
{
    return Regles::lancerDes(nbDes);
}

bool Reglesbase::comparer(int Datt,int Ddef)
{
    return Regles::comparer(Datt,Ddef);
}
